﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectManagement.Domain.Entities;
using ProjectManagement.Domain.Abstract;
using NHibernate;
using ProjectManagement.Domain.AutoMappings;
using NHibernate.Linq;

namespace ProjectManagement.Domain.Services
{
	public class CompanyService:IRepository<Company>
	{
		private static ISessionFactory factory = NHibernateHelper.CreateSessionFactory();
		private static ISession session = factory.OpenSession();
		public void Save(Company entity)
		{
			using (ITransaction trans = session.BeginTransaction())
			{
				session.Save(entity);
				trans.Commit();
				session.Flush();
			}
		}

		public void Remove(Company entity)
		{
			using (ITransaction trans = session.BeginTransaction())
			{
				session.Delete(entity);
				trans.Commit();
			}
		}

		public void Update(Company entity)
		{
			using (ITransaction trans = session.BeginTransaction())
			{
				session.Update(entity);
				trans.Commit();
			}
		}

		public IEnumerable<Company> QueryAll()
		{
			return session.Query<Company>()
				.ToList();
		}

		public Company Get(int id)
		{
			return session.Get<Company>(id);
		}

		public void SaveOrUpdate(Company entity)
		{
			using (ITransaction trans = session.BeginTransaction())
			{
				session.SaveOrUpdate(entity);
				trans.Commit();
			}
		}

		public void DeleteCompanyById(int id)
		{
			using (ITransaction trans = session.BeginTransaction())
			{
				var model = Get(id);
				session.Delete(model);
				trans.Commit();
			}
		}
	}
}
